package in.muton.edushare.pages.publicpages.coursepages;

import in.muton.edushare.url.EduShareURLPanel;
import in.muton.edushare.wicket.models.CayenneList;
import in.muton.edushare.wicket.models.CayenneModel;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.vo.Courses;
import com.edushare.common.vo.PrerequisiteLink;

public class CoursePublic extends CourseBasePublic {

	public CoursePublic(PageParameters params) {
		super(params);
		add(new Label("lhr", new PropertyModel<String>(this, "lhr")));
		add(new Label("thr", new PropertyModel<String>(this, "thr")));
		add(new Label("phr", new PropertyModel<String>(this, "phr")));
		add(new Label("credits", new PropertyModel<String>(this, "credits")));
		add(new Label("strength", new PropertyModel<String>(this, "strength")));

		final Label preLabel = new Label("AndOr", new PropertyModel<String>(
				this, "prereqCondn"));
		ListView<IModel<PrerequisiteLink>> preList = new ListView<IModel<PrerequisiteLink>>(
				"prereqList", new CayenneList<PrerequisiteLink>(
						getCourseModel().getObject().getPrerequisiteLink())) {

			private static final long serialVersionUID = -5778839180704121111L;

			@Override
			protected void populateItem(ListItem<IModel<PrerequisiteLink>> item) {
				item.add(new EduShareURLPanel("prereqItem",
						new CayenneModel<Courses>(item.getModelObject()
								.getObject().getPrerequisite())));
			}

			@Override
			protected void onBeforeRender() {
				if (getList() == null || getList().size() < 1) {
					preLabel.setDefaultModelObject("No prerequisite");
				}
				super.onBeforeRender();
			}

		};

		add(preList);
		add(preLabel);
		add(new Label("courseDesc", new PropertyModel<String>(getCourseModel(),
				"courseDesc")).setEscapeModelStrings(false));
	}

	public String getPrereqCondn() {
		if (getCourseModel().getObject().getPrerequisitesCondition() == null) {
			return "No prerequisite";
		} else if (getCourseModel().getObject().getPrerequisitesCondition() == Courses.PREREQUISITE_COURSES_AND) {
			return "All of:";
		} else if (getCourseModel().getObject().getPrerequisitesCondition() == Courses.PREREQUISITE_COURSES_OR) {
			return "At least one of:";
		}
		return "No prerequisite";
	}

	@Override
	public String getPageName(PageParameters params) {
		return "courseBasePublic";
	}

	public String getLhr() {
		return (getCourseModel().getObject().getLectureHours() != Courses.NOT_AVAILABLE) ? getCourseModel()
				.getObject().getLectureHours().toString()
				: "N/A";
	}

	public String getThr() {
		return (getCourseModel().getObject().getTutorialHours() != Courses.NOT_AVAILABLE) ? getCourseModel()
				.getObject().getTutorialHours().toString()
				: "N/A";
	}

	public String getPhr() {
		return (getCourseModel().getObject().getPracticalHours() != Courses.NOT_AVAILABLE) ? getCourseModel()
				.getObject().getPracticalHours().toString()
				: "N/A";
	}

	public String getCredits() {
		return (getCourseModel().getObject().getCreditString());
	}

	public String getStrength() {
		return (getCourseModel().getObject().getStrength() != Courses.NOT_AVAILABLE) ? getCourseModel()
				.getObject().getStrength().toString()
				: "N/A";
	}
}
